package Controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"log"
	"m/Global"
	"m/model"
	"net/http"
)

func RegisterTeacher(context *gin.Context) {
	// 使用结构体来获取参数
	var requsetTeacher = model.Teacher{}
	json.NewDecoder(context.Request.Body).Decode(&requsetTeacher)
	fmt.Println("获取到的数据：", requsetTeacher.Tno, requsetTeacher.Tname, requsetTeacher.Tlevel, requsetTeacher.Tcharacter)

	// 获取参数
	fmt.Println("开始获取教师参数...")
	tno := requsetTeacher.Tno
	tname := requsetTeacher.Tname
	tlevel := requsetTeacher.Tlevel
	tcharacter := requsetTeacher.Tcharacter
	// 数据验证
	fmt.Println("开始验证教师数据...")
	fmt.Println("教师号：", tno, "教师名：", tname, "教师等级：", tlevel, "教师特征：", tcharacter)
	if len(tno) == 0 {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":421, "msg":"教师号不可为空！"})
		fmt.Println("发现教师号为空")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师号不能为空",
		})
		return
	}
	if len(tname) == 0 {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"教师名不可为空！"})
		fmt.Println("发现教师名为空")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师名不能为空",
		})
		return
	}
	fmt.Println(tno, tname, tlevel, tcharacter)
	log.Println(tno, tname, tlevel, tcharacter)
	// 判断教师号是否存在
	//tempTno, err := strconv.Atoi(tno)
	if isTnoExists(Global.Db, tno) == true {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"教师号已经存在！"})
		// 直接返回，不执行下面的create了
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师号已经存在",
		})
		return
	}
	// 创建教师
	//if err != nil {
	//	fmt.Println("错误")
	//	return
	//}
	newTeacher := model.Teacher{
		Tno: tno,
		Tname: tname,
		Tlevel: tlevel,
		Tcharacter: tcharacter,
	}
	Global.Db.Create(&newTeacher)
	// 返回结果
	context.JSON(200, gin.H{
		"msg" : "插入教师成功",
	})
}

func ShowTeacher(context *gin.Context)  {
	fmt.Println("进入showteacher")
	var teacher []model.Teacher
	err := Global.Db.Find(&teacher).Error
	if err != nil {
		fmt.Println("查询全体教师出错！")
		return
	}
	fmt.Println("数据库查到的全体教师信息：", teacher)

	//token, err := Global.ReaeaseTokec3(teacher)
	context.JSON(http.StatusOK, teacher)
}

func CountTeacher(context *gin.Context)  {
	// 调用存储过程
	var count int
	Global.Db.Raw("select count_teacher()").First(&count)
	fmt.Println("教师数量：", count)
	context.JSON(200, gin.H{
		"code":200,
		"msg":"返回教师数量成功",
		"data":count,
	})
}

func EditTeacher(context *gin.Context) {
	fmt.Println("进入editteacher")
	var requestTeacher = model.Teacher{}
	json.NewDecoder(context.Request.Body).Decode(&requestTeacher)
	fmt.Println("即将编辑教师号：", requestTeacher.Tno)

	if len(requestTeacher.Tno) < 1 {
		fmt.Println("教师号不能为空！", len(requestTeacher.Tno))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师号不能为空",
		})
		return
	}
	if len(requestTeacher.Tname) < 1 {
		fmt.Println("教师姓名不能为空！", len(requestTeacher.Tname))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师姓名不能为空",
		})
		return
	}
	if len(requestTeacher.Tlevel) < 1 {
		fmt.Println("教师等级不能为空！", len(requestTeacher.Tlevel))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师等级不能为空",
		})
		return
	}
	if len(requestTeacher.Tcharacter) < 1 {
		fmt.Println("教师特长不能为空！", len(requestTeacher.Tcharacter))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师特长不能为空",
		})
		return
	}

	// 教师号是否存在
	var teacher model.Teacher
	Global.Db.Where("tno = ?", requestTeacher.Tno).First(&teacher)
	fmt.Println("teacher.Tno=", teacher.Tno)
	if teacher.Tno == "" {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"找不到此教师",
		})
		return
	}
	//Global.Db.Save(&requestTeacher)
	//Global.Db.Model(&requestTeacher).Updates(model.Teacher{
	//	Tno: requestTeacher.Tno,
	//	Tname: requestTeacher.Tname,
	//	Tlevel: requestTeacher.Tlevel,
	//	Tcharacter: requestTeacher.Tcharacter,
	//})
	err := Global.Db.Where("tno = ?", requestTeacher.Tno).Updates(&requestTeacher).Error
	if err != nil {
		fmt.Println("更新教师出错")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"更新教师出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code":200,
		"msg":"更新成功",
	})

}

func DeleteTeacher(context *gin.Context)  {
	fmt.Println("进入deleteteacher")
	var requestTeacher = model.Teacher{}
	json.NewDecoder(context.Request.Body).Decode(&requestTeacher)
	fmt.Println("即将删除教师编号：", requestTeacher.Tno)

	if len(requestTeacher.Tno) < 1 {
		fmt.Println("教师号不能为空！", len(requestTeacher.Tno))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"教师号不能为空",
		})
		return
	}
	// 教师号是否存在
	var teacher model.Teacher
	Global.Db.Where("tno = ?", requestTeacher.Tno).First(&teacher)
	fmt.Println("teacher.Tno=", teacher.Tno)
	if teacher.Tno == "" {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"找不到此教师",
		})
		return
	}
	// 首先删除有这个老师的课程
	var course = model.Course{
		Tno: requestTeacher.Tno,
	}
	var err error
	err = Global.Db.Where("tno = ?", requestTeacher.Tno).Delete(&course).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"删除此教师所教授的课程出错",
		})
		return
	}

	// 其次删除这个老师
	err = Global.Db.Where("tno = ?", requestTeacher.Tno).Delete(&teacher).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"删除教师出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code":200,
		"msg":"删除成功",
	})
}

func ShowTeacherSelect(context *gin.Context)  {
	fmt.Println("进入showteacheerselect")
	var requestTeacher = model.Teacher{}
	json.NewDecoder(context.Request.Body).Decode(&requestTeacher)
	fmt.Println("即将搜索教师号：", requestTeacher.Tno)

	if len(requestTeacher.Tno) < 1 {
		fmt.Println("教师号不能为空！", len(requestTeacher.Tno))
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code":422,
			"msg":"教师号为空",
		})
		return
	}
	// 教师号是否存在
	var teacher []model.Teacher
	Global.Db.Where("tno = ?", requestTeacher.Tno).First(&teacher)
	fmt.Println("teacher.Tno=", teacher[0].Tno)
	if teacher[0].Tno == "" {
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code":422,
			"msg":"找不到此教师！",
		})
		return
	}
	context.JSON(http.StatusOK, teacher)

}

func ShowTeacherDelete(context *gin.Context)  {
	fmt.Println("进入showteacherdelete")
	//var requesetTeacher = model.Teacher{}
}

func isTnoExists(db *gorm.DB, tno string) bool {
	var teacher model.Teacher
	err := db.Where("tno = ?", tno).Find(&teacher).Error
	if err != nil {
		fmt.Println("err执行")
		return false
	}
	fmt.Println("数据库返回的教师号：", teacher.Tno)
	if teacher.Tno != "" {// 找到重复的教师号
		fmt.Println("找到重复的教师号为：", teacher.Tno)
		fmt.Println("教师执行了if")
		return true
	} else {
		fmt.Println("教师执行了else")
		return false
	}
}
